using NaughtyAttributes;
using SimpleJSON;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UI;

namespace Gj
{
    public class ContentTextHelper : NodeHelper
    {
        public RectTransform imageRect;
        public RectTransform textRect;
        public Text text;
        public bool local;
        public bool online;
        public bool autoWidth;
        public int maxWidth;
        public int minWidth;
        public int maxHeight;
        public int minHeight;
        public int lineHeight;
        public string localText;
        public float horizontalPadding;
        public float verticalPadding;
        public AdaptiveSize adaptiveSize;
        private Language _language = Language.None;
        private string _s;

        protected override BindDataType bindDataType => BindDataType.String;

        private void CheckLanguage()
        {
            if (_language != Localization.Instance.language)
            {
                _language = Localization.Instance.language;
                _s = null;
                UpdateLocal();
            }
        }

        public override void SetData(JSONObject json)
        {
            CheckLanguage();
            base.SetData(json);
        }

        protected override void BindData(string s)
        {
            if (s == null) s = "";
            UpdateText(s);
        }

        protected void UpdateText(string s)
        {
            if (_s != null && _s.Equals(s)) return;
            _s = s;
            text.text = GetText(s).Replace("\\n", "\n");

            Refresh();
        }

        private string GetText(string s)
        {
            // Debug.LogErrorFormat("{0} {1} {2}", gameObject.name, gameObject.transform.parent.name ,s);
            if (online && !StringTools.IsEmpty(s))
            {
                var o = Localization.Instance.GetOnlineValue(s);
                s = o == "" ? s : o;
            }

            return s;
        }

        [Button]
        public void Text()
        {
            BindData(text.text);
        }

        [Button]
        public void UpdateLocal()
        {
            if (localText == "") return;
            UpdateText(localText);
        }

        [Button]
        public void Auto()
        {
            float w = 0;
            float h = 0;
            float width = 0;
            float line = 0;
            var tg = text.cachedTextGeneratorForLayout;
            var settings = text.GetGenerationSettings(Vector2.zero);
            var lines = text.text.Split('\n');
            for (var i = 0; i < lines.Length; i++)
            {
                var _w = tg.GetPreferredWidth(lines[i], settings) / text.pixelsPerUnit;
                width = _w > width ? _w : width;
            }

            var _maxWidth = maxWidth - horizontalPadding * 2;
            var _minWidth = minWidth - horizontalPadding * 2;
            if (_minWidth < 0) _minWidth = 0;
            var _maxHeight = maxHeight - verticalPadding * 2;
            var _minHeight = minHeight - verticalPadding * 2;
            if (_minHeight < 0) _minHeight = 0;

            if (autoWidth)
            {
                w = width;
                line = lines.Length;
            }
            else
            {
                w = width > _maxWidth ? _maxWidth : width < _minWidth ? _minWidth : width;

                for (var i = 0; i < lines.Length; i++)
                {
                    var _w = tg.GetPreferredWidth(lines[i], settings) / text.pixelsPerUnit;
                    width = _w > width ? _w : width;
                    line += math.ceil(width / _maxWidth);
                }
            }

            h = width == 0 ? _minHeight : line * lineHeight;

            textRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, w);
            textRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, h);

            imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, w + horizontalPadding * 2);
            imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, h > 0 ? h + verticalPadding * 2 : 0);
        }

        [Button]
        public void Refresh()
        {
            Auto();
            if (adaptiveSize != null) adaptiveSize.Refresh();
        }

        [Button]
        public void SaveLocalByOnline()
        {
            if (text.text != "")
            {
                localText = text.text;
                online = true;
            }
        }

        public void Show()
        {
            gameObject.SetActive(true);
        }

        public void Hide()
        {
            gameObject.SetActive(false);
        }
    }
}